﻿/*
* This software is the confidential and proprietary
* information of NetBay
* Such confidential information should not be disclosed
* NetBay copyright(c) 2010
*/
#region Using
using System;
using System.Collections.Generic;
using NetBay.Core.BusinessEntities;
using Rio.Framework.Historical;
using System.Data;

#endregion

namespace Rio.Flow.Business.Repository.Translator
{
    public partial class MessageFlowTranslator
    {
        /// <summary>
        /// Translate a bunch of DocumentType DataReader to DocumentType object
        /// </summary>
        /// <param name="p_oIDataReader">The dataReader that contains the rows</param>
        /// <returns>A list of Message Object</returns>
        public static List<MessageFlow> TranslateMessageFlows(IDataReader p_oIDataReader)
        {
            var l_lstMessageFlows = new List<MessageFlow>();

            try
            {
                using (p_oIDataReader)
                {
                    // Read all rows
                    while (p_oIDataReader.Read())
                    {
                        TranslateOne(p_oIDataReader, l_lstMessageFlows);
                    }
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_lstMessageFlows;
        }

        public static MessageFlow TranslateMessageFlow(IDataReader p_oIDataReader)
        {
            var l_lstMessageFlow = new MessageFlow();

            try
            {
                using (p_oIDataReader)
                {
                    // Read all rows
                    while (p_oIDataReader.Read())
                    {
                        TranslateMessageById(p_oIDataReader, l_lstMessageFlow);
                    }
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_lstMessageFlow;
        }

        private static void TranslateMessageById(IDataReader p_oIDataReader, MessageFlow l_lstMessageFlow)
        {
            var l_oMessageFlow = new MessageFlow();

            try
            {
                while (p_oIDataReader.Read())
                {
                    l_oMessageFlow.Identifier = Convert.ToInt32(p_oIDataReader["MSG_FLOW_ID"].ToString());
                    l_oMessageFlow.DestinataireList = p_oIDataReader["MSG_FLOW_GROUP_DESTINATAIRES"].ToString();
                    l_oMessageFlow.Objet = p_oIDataReader["MSG_FLOW_OBJET"].ToString();
                    l_oMessageFlow.Memo = p_oIDataReader["MSG_FLOW_MEMO"].ToString();
                    l_oMessageFlow.Expedition =
                    FieldRow.Translate<DateTime>(p_oIDataReader["MSG_FLOW_DATE_EXPEDITION"].ToString());
                    l_oMessageFlow.LastReadDate =
                    FieldRow.Translate<DateTime>(p_oIDataReader["MSG_FLOW_LAST_READ_DATE"].ToString());
                    l_oMessageFlow.LastActionDate =
                    FieldRow.Translate<DateTime>(p_oIDataReader["MSG_FLOW_LAST_ACTION_DATE"].ToString());
                    l_oMessageFlow.SendAlertByEmail =
                    Convert.ToBoolean(Convert.ToInt16(p_oIDataReader["MSG_FLOW_ALERT_BY_MAIL"].ToString()));
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }
        }

        private static void TranslateOne(IDataReader p_oIDataReader, List<MessageFlow> p_oMessageFlows)
        {
            var l_oMessageFlow = new MessageFlow();
            var l_oEmetteur = new User();
            var l_oMessageFlowDocumentList = new List<Document>();

            try
            {
                //Fill in the email receiver information (Destinataire)
                l_oEmetteur.UserId = Convert.ToInt32(p_oIDataReader["ruser_no"].ToString());
                l_oEmetteur.Email = p_oIDataReader["ruser_email"].ToString();
                l_oEmetteur.Login = p_oIDataReader["ruser_login"].ToString();
                l_oEmetteur.LastName = p_oIDataReader["ruser_nom"].ToString();
                l_oEmetteur.FirstName = p_oIDataReader["ruser_prenom"].ToString();

                l_oMessageFlow.Identifier = Convert.ToInt32(p_oIDataReader["MSG_FLOW_ID"].ToString());
                l_oMessageFlow.Emetteur = l_oEmetteur;
                l_oMessageFlow.DocumentList = l_oMessageFlowDocumentList;
                l_oMessageFlow.DestinataireList = p_oIDataReader["MSG_FLOW_GROUP_DESTINATAIRES"].ToString();
                l_oMessageFlow.Objet = p_oIDataReader["MSG_FLOW_OBJET"].ToString();
                l_oMessageFlow.Memo = p_oIDataReader["MSG_FLOW_MEMO"].ToString();
                l_oMessageFlow.Expedition =
                FieldRow.Translate<DateTime>(p_oIDataReader["MSG_FLOW_DATE_EXPEDITION"].ToString());
                l_oMessageFlow.LastReadDate =
                FieldRow.Translate<DateTime>(p_oIDataReader["MSG_FLOW_LAST_READ_DATE"].ToString());
                l_oMessageFlow.LastActionDate =
                FieldRow.Translate<DateTime>(p_oIDataReader["MSG_FLOW_LAST_ACTION_DATE"].ToString());
                l_oMessageFlow.Chrono = p_oIDataReader["CHRONO_LIBELLE"].ToString();
                if (
                    (Equals(p_oIDataReader["rg_id"], DBNull.Value) == false &&
                     (string.Empty.Equals(p_oIDataReader["rg_id"]) == false))
                    )
                {
                    l_oMessageFlow.GroupId = Convert.ToInt64(p_oIDataReader["rg_id"].ToString());
                    l_oMessageFlow.RguId = Convert.ToInt64(p_oIDataReader["rgu_id"].ToString());
                }
                l_oMessageFlow.MyDocEtat = Convert.ToInt32(p_oIDataReader["MY_DOC_ETAT"].ToString());

                l_oMessageFlow.SendAlertByEmail =
                Convert.ToBoolean(Convert.ToInt16(p_oIDataReader["MSG_FLOW_ALERT_BY_MAIL"].ToString()));

                // All members are readed, so save this document to collection
                p_oMessageFlows.Add(l_oMessageFlow);
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }
        }

        /// <summary>
        /// Translate a bunch of DocumentType DataReader to DocumentType object
        /// </summary>
        /// <param name="p_oIDataReader">The dataReader that contains the rows</param>
        /// <returns>A list of Message Object</returns>
        public static List<StateMessageFlow> TranslateStateMessageFlows(IDataReader p_oIDataReader)
        {
            var l_lstStateMessageFlows = new List<StateMessageFlow>();

            try
            {
                using (p_oIDataReader)
                {
                    // Read all rows
                    while (p_oIDataReader.Read())
                    {
                        TranslateStateMessageFlowOne(p_oIDataReader, l_lstStateMessageFlows);
                    }
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_lstStateMessageFlows;
        }

        private static void TranslateStateMessageFlowOne(IDataReader p_oIDataReader, List<StateMessageFlow> p_oStateMessageFlows)
        {
            var l_oStateMessageFlow = new StateMessageFlow();

            try
            {
                if (p_oIDataReader["RD_NO"] != null &&
                    !string.Empty.Equals(p_oIDataReader["RD_NO"].ToString()))
                {
                    l_oStateMessageFlow.RdNo = Convert.ToInt64(p_oIDataReader["RD_NO"].ToString());
                }
                if (p_oIDataReader["RGU_ID"] != null &&
                    !string.Empty.Equals(p_oIDataReader["RGU_ID"].ToString()))
                {
                    l_oStateMessageFlow.RguId = Convert.ToInt64(p_oIDataReader["RGU_ID"].ToString());
                }
                if (p_oIDataReader["DOC_ETAT_ID"] != null &&
                    !string.Empty.Equals(p_oIDataReader["DOC_ETAT_ID"].ToString()))
                {
                    l_oStateMessageFlow.StateId = Convert.ToInt64(p_oIDataReader["DOC_ETAT_ID"].ToString());
                }
                if (p_oIDataReader["MSG_FLOW_ID"] != null &&
                    !string.Empty.Equals(p_oIDataReader["MSG_FLOW_ID"].ToString()))
                {
                    l_oStateMessageFlow.FlowId = Convert.ToInt64(p_oIDataReader["MSG_FLOW_ID"].ToString());
                }
                if (p_oIDataReader["DATE_ADD"] != null &&
                    !string.Empty.Equals(p_oIDataReader["DATE_ADD"].ToString()))
                {
                    l_oStateMessageFlow.DateAdded = ConvertField.ToDateTime(p_oIDataReader["DATE_ADD"].ToString());
                }
                if (p_oIDataReader["DOC_ETAT_LIBELLE"] != null &&
                    !string.Empty.Equals(p_oIDataReader["DOC_ETAT_LIBELLE"].ToString()))
                {
                    l_oStateMessageFlow.WordingStateDoc = p_oIDataReader["DOC_ETAT_LIBELLE"].ToString();
                }
                if (p_oIDataReader["RUSER_NOM"] != null &&
                    !string.Empty.Equals(p_oIDataReader["RUSER_NOM"].ToString()))
                {
                    l_oStateMessageFlow.FirstName = p_oIDataReader["RUSER_NOM"].ToString();
                }
                if (p_oIDataReader["RUSER_PRENOM"] != null &&
                    !string.Empty.Equals(p_oIDataReader["RUSER_PRENOM"].ToString()))
                {
                    l_oStateMessageFlow.LastName = p_oIDataReader["RUSER_PRENOM"].ToString();
                }
                if (p_oIDataReader["DOC_ETAT_GROUP_COMMENT"] != null &&
                    !string.Empty.Equals(p_oIDataReader["DOC_ETAT_GROUP_COMMENT"].ToString()))
                {
                    l_oStateMessageFlow.Comment = p_oIDataReader["DOC_ETAT_GROUP_COMMENT"].ToString();
                }
                else
                {
                    l_oStateMessageFlow.Comment = string.Empty;
                }
                // All members are readed, so save this object to collection 
                p_oStateMessageFlows.Add(l_oStateMessageFlow);
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }
        }

        /// <summary>
        /// Translate a bunch of StateDoc DataReader to StateDoc object
        /// </summary>
        /// <param name="p_oIDataReader">The dataReader that contains the rows</param>
        /// <returns>A list of Message Object</returns>
        public static List<StateDoc> TranslateStateDocs(IDataReader p_oIDataReader)
        {
            var l_lstStateDocs = new List<StateDoc>();

            try
            {
                using (p_oIDataReader)
                {
                    while (p_oIDataReader.Read())
                    {
                        TranslateStateDocOne(p_oIDataReader, l_lstStateDocs);
                    }
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_lstStateDocs;
        }

        private static void TranslateStateDocOne(IDataReader p_oIDataReader, List<StateDoc> p_oStateMessageFlows)
        {
            var l_oStateDoc = new StateDoc();

            try
            {
                l_oStateDoc.StateId = Convert.ToInt64(p_oIDataReader["DOC_ETAT_ID"].ToString());
                l_oStateDoc.WordingStateDoc = p_oIDataReader["DOC_ETAT_LIBELLE"].ToString();

                // All members are readed, so save this object to collection
                p_oStateMessageFlows.Add(l_oStateDoc);
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }
        }
    }
}